#include "AppStateMenu.h"
#include "AppStateManager.h"
#include "Random.h"
#include <SDL.h>

void CAppStateMenu::OnKeyNameWrite(SDLKey sym)
{
	switch(sym)
	{
		case SDLK_BACKSPACE: 
		{
			if(!name.empty()) 
				name.erase(name.length() - 1);
			break;
		} 
		case SDLK_ESCAPE:
		{
			name = "Player";
			mainChoice = MAINMENU_CHOICE;
		}

		case SDLK_RIGHT: //If a right key is pressed go to the next item in items.cpp.
		{
			
			if(itemID == itemInstance.effects->size()-2) //Makes sure the current choosen item is not bigger than the itemArray in items.cpp.
			{
				itemID = 0;

				stats[0] = "";
				stats[1] = "";
				stats[2] = "";

				//Gets the different effects of the choosen item, and stores them in stats.
				std::string effects =  itemInstance.effects[itemID];
				
				
				stats[0]+= effects.at(0);
				stats[0]+= effects.at(1);
				stats[0]+= effects.at(2);
							
				stats[1]+= effects.at(4);
				stats[1]+= effects.at(5);
				stats[1]+= effects.at(6);

				stats[2]+= effects.at(8);
				stats[2]+= effects.at(9);
				stats[2]+= effects.at(10);
			}
			else
			{	
				stats[0] = "";
				stats[1] = "";
				stats[2] = "";

				CAppState::itemID ++;

				//Gets the different effects of the choosen item, and stores them in stats.
				std::string effects =  itemInstance.effects[itemID];
				
				
				stats[0]+= effects.at(0);
				stats[0]+= effects.at(1);
				stats[0]+= effects.at(2);
							
				stats[1]+= effects.at(4);
				stats[1]+= effects.at(5);
				stats[1]+= effects.at(6);

				stats[2]+= effects.at(8);
				stats[2]+= effects.at(9);
				stats[2]+= effects.at(10);
					
			}
			CAppState::itemName = itemInstance.names[itemID]; //Stores the name of the current choosen item in itemName.
			break;
		}

		case SDLK_LEFT: //If a left key is pressed go to the previous item in items.cpp.
		{
			if(itemID == 0) //Makes sure the current choosen item is not smaller than the itemArray in items.cpp.
			{
				itemID = itemInstance.effects->size()-2;
				stats[0] = "";
				stats[1] = "";
				stats[2] = "";

				//Gets the different effects of the choosen item, and stores them in stats.
				std::string effects =  itemInstance.effects[itemID];
				
				
				stats[0]+= effects.at(0);
				stats[0]+= effects.at(1);
				stats[0]+= effects.at(2);
							
				stats[1]+= effects.at(4);
				stats[1]+= effects.at(5);
				stats[1]+= effects.at(6);

				stats[2]+= effects.at(8);
				stats[2]+= effects.at(9);
				stats[2]+= effects.at(10);
			}
			else
			{
				stats[0] = "";
				stats[1] = "";
				stats[2] = "";

				CAppState::itemID --;

				//Gets the different effects of the choosen item, and stores them in stats.
				std::string effects =  itemInstance.effects[itemID];
				
				
				stats[0]+= effects.at(0);
				stats[0]+= effects.at(1);
				stats[0]+= effects.at(2);
							
				stats[1]+= effects.at(4);
				stats[1]+= effects.at(5);
				stats[1]+= effects.at(6);

				stats[2]+= effects.at(8);
				stats[2]+= effects.at(9);
				stats[2]+= effects.at(10);
			}

			CAppState::itemName = itemInstance.names[itemID];
			break;
		}

		case SDLK_RETURN:
		{	
			if(items[4] != NULL) //if all items have been choosen, go to the lobby
			{
				mainChoice = MAINMENU_CHOICE;
			}
			
			//Adds the choosen item to your itemList.
		
			//This checks if the items in the char is null or already added, then adds the item.
			if(items[0] == NULL) 
			{
				items[0] = '0'+itemID;
				name = "1/5";
				if(itemID == itemInstance.effects->size()-2)
			{
				itemID = 0;

				stats[0] = "";
				stats[1] = "";
				stats[2] = "";

				std::string effects =  itemInstance.effects[itemID];
				
				
				stats[0]+= effects.at(0);
				stats[0]+= effects.at(1);
				stats[0]+= effects.at(2);
							
				stats[1]+= effects.at(4);
				stats[1]+= effects.at(5);
				stats[1]+= effects.at(6);

				stats[2]+= effects.at(8);
				stats[2]+= effects.at(9);
				stats[2]+= effects.at(10);
			}
			else
			{	
				stats[0] = "";
				stats[1] = "";
				stats[2] = "";

				CAppState::itemID ++;
				std::string effects =  itemInstance.effects[itemID];
				
				
				stats[0]+= effects.at(0);
				stats[0]+= effects.at(1);
				stats[0]+= effects.at(2);
							
				stats[1]+= effects.at(4);
				stats[1]+= effects.at(5);
				stats[1]+= effects.at(6);

				stats[2]+= effects.at(8);
				stats[2]+= effects.at(9);
				stats[2]+= effects.at(10);
					
			}
			CAppState::itemName = itemInstance.names[itemID];
			}
			else if(items[1] == NULL)
			{
				items[1] = '0'+itemID;
				name = "2/5";
				if(itemID == itemInstance.effects->size()-2)
			{
				itemID = 0;

				stats[0] = "";
				stats[1] = "";
				stats[2] = "";

				std::string effects =  itemInstance.effects[itemID];
				
				
				stats[0]+= effects.at(0);
				stats[0]+= effects.at(1);
				stats[0]+= effects.at(2);
							
				stats[1]+= effects.at(4);
				stats[1]+= effects.at(5);
				stats[1]+= effects.at(6);

				stats[2]+= effects.at(8);
				stats[2]+= effects.at(9);
				stats[2]+= effects.at(10);
			}
			else
			{	
				stats[0] = "";
				stats[1] = "";
				stats[2] = "";

				CAppState::itemID ++;
				std::string effects =  itemInstance.effects[itemID];
				
				
				stats[0]+= effects.at(0);
				stats[0]+= effects.at(1);
				stats[0]+= effects.at(2);
							
				stats[1]+= effects.at(4);
				stats[1]+= effects.at(5);
				stats[1]+= effects.at(6);

				stats[2]+= effects.at(8);
				stats[2]+= effects.at(9);
				stats[2]+= effects.at(10);
					
			}
			CAppState::itemName = itemInstance.names[itemID];
			}
			else if(items[2] == NULL)
			{
				items[2] = '0'+itemID;
				name = "3/5";
				if(itemID == itemInstance.effects->size()-2)
			{
				itemID = 0;

				stats[0] = "";
				stats[1] = "";
				stats[2] = "";

				std::string effects =  itemInstance.effects[itemID];
				
				
				stats[0]+= effects.at(0);
				stats[0]+= effects.at(1);
				stats[0]+= effects.at(2);
							
				stats[1]+= effects.at(4);
				stats[1]+= effects.at(5);
				stats[1]+= effects.at(6);

				stats[2]+= effects.at(8);
				stats[2]+= effects.at(9);
				stats[2]+= effects.at(10);
			}
			else
			{	
				stats[0] = "";
				stats[1] = "";
				stats[2] = "";

				CAppState::itemID ++;
				std::string effects =  itemInstance.effects[itemID];
				
				
				stats[0]+= effects.at(0);
				stats[0]+= effects.at(1);
				stats[0]+= effects.at(2);
							
				stats[1]+= effects.at(4);
				stats[1]+= effects.at(5);
				stats[1]+= effects.at(6);

				stats[2]+= effects.at(8);
				stats[2]+= effects.at(9);
				stats[2]+= effects.at(10);
					
			}
			CAppState::itemName = itemInstance.names[itemID];
			}
			else if(items[3] == NULL)
			{
				items[3] = '0'+itemID;
				name = "4/5";
				if(itemID == itemInstance.effects->size()-2)
			{
				itemID = 0;

				stats[0] = "";
				stats[1] = "";
				stats[2] = "";

				std::string effects =  itemInstance.effects[itemID];
				
				
				stats[0]+= effects.at(0);
				stats[0]+= effects.at(1);
				stats[0]+= effects.at(2);
							
				stats[1]+= effects.at(4);
				stats[1]+= effects.at(5);
				stats[1]+= effects.at(6);

				stats[2]+= effects.at(8);
				stats[2]+= effects.at(9);
				stats[2]+= effects.at(10);
			}
			else
			{	
				stats[0] = "";
				stats[1] = "";
				stats[2] = "";

				CAppState::itemID ++;
				std::string effects =  itemInstance.effects[itemID];
				
				
				stats[0]+= effects.at(0);
				stats[0]+= effects.at(1);
				stats[0]+= effects.at(2);
							
				stats[1]+= effects.at(4);
				stats[1]+= effects.at(5);
				stats[1]+= effects.at(6);

				stats[2]+= effects.at(8);
				stats[2]+= effects.at(9);
				stats[2]+= effects.at(10);
					
			}
			CAppState::itemName = itemInstance.names[itemID];
			}
			else if(items[4] == NULL)
			{
				items[4] = '0'+itemID;	
				name = "";
			}
			else
			{
				if(name.size()<8 )
				{
					if(sym >= SDLK_a && sym <= SDLK_z)
						name += sym;
				}
			}
			break;
		}
		default: 
		{
			if(items[4] == NULL)
			{
			
			}
			else
			{
				if(name.size()<8 ) //If the size of the name is not bigger than 7, add the new char to your name.
				{
					if(sym >= SDLK_a && sym <= SDLK_z)
						name += sym;
				}
				else
				{
				
				}
			}
		}
		
	}			
}

void CAppStateMenu::OnRenderNameChoice(SDL_Surface* Surf_Display)
{
	SDL_Rect NameRecti;
	NameRecti.x = 150;
	NameRecti.y = 150;
	NameRecti.w = 200;
	NameRecti.h = 30;

	SDL_Surface* Text_Solid;
	Text_Solid = NULL;

	SDL_Rect TextPos;
	TextPos.x = 204;
	TextPos.y = 320;

	SDL_Colour black = {0,0,0};
	SDL_BlitSurface(NameBox, NULL, Surf_Display, &NameRecti);
	if(items[0] == NULL)
	{
		name = "0/5";
	}
	Text_Solid = TTF_RenderText_Blended(IPFont, name.c_str(), black);
	SDL_BlitSurface(Text_Solid, NULL, Surf_Display, &TextPos);
	SDL_FreeSurface(Text_Solid);
	if(itemName == "")
	{
		CAppState::itemName = itemInstance.names[0];
	}
	Text_Solid = TTF_RenderText_Blended(IPFont, itemName.c_str(), black);
	TextPos.y = 235;
	SDL_BlitSurface(Text_Solid, NULL, Surf_Display, &TextPos);
	SDL_FreeSurface(Text_Solid);

	Text_Solid = TTF_RenderText_Blended(CSurface::font, stats[0].c_str(), black);
	TextPos.y = 274;
	TextPos.x+=2;
	SDL_BlitSurface(Text_Solid, NULL, Surf_Display, &TextPos);
	SDL_FreeSurface(Text_Solid);

	Text_Solid = TTF_RenderText_Blended(CSurface::font, stats[2].c_str(), black);
	TextPos.x+=85;
	SDL_BlitSurface(Text_Solid, NULL, Surf_Display, &TextPos);
	SDL_FreeSurface(Text_Solid);

	Text_Solid = TTF_RenderText_Blended(CSurface::font, stats[1].c_str(), black);
	TextPos.x+=85;
	SDL_BlitSurface(Text_Solid, NULL, Surf_Display, &TextPos);
	SDL_FreeSurface(Text_Solid);
}